package zucc.edu.cn.chengchengparttime.Mysignup;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatButton;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import zucc.edu.cn.chengchengparttime.R;
import zucc.edu.cn.chengchengparttime.Signup;
import zucc.edu.cn.chengchengparttime.Text;
import zucc.edu.cn.chengchengparttime.dbHomeHelper;

public class MysignupAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int NORMAL_VIEW=0; //兼职列表

    private static final String DEBUG_TAG="Chengcheng Log";
    private SQLiteDatabase mDatabase;// 数据库实例
    dbHomeHelper homeHelper;
    View view;

    private Context context;
    private List<Signup> dataList;

    public MysignupAdapter(List<Signup> dataList, Context context, SQLiteDatabase mDatabase, View view){
        this.context=context;
        this.dataList=dataList;
        this.mDatabase=mDatabase;
        this.view=view;
    }

    public class NormalViewHolder extends RecyclerView.ViewHolder{
        public NormalViewHolder(View itemView){
            super(itemView);
            itemView.setOnClickListener(v -> {
            });
        }
        //数据封装
        public TextView workname;
        public TextView worktimeandarea;
        public TextView workstatus;
        public TextView salaryperhour;
        public TextView paymentway;
        public AppCompatButton btn_getPhone;
        public AppCompatButton btn_cancleSignup;
        //getter和setter方法
        public TextView getWorkname() {
            return workname;
        }

        public void setWorkname(TextView workname) {
            this.workname = workname;
        }

        public TextView getWorktimeandarea() {
            return worktimeandarea;
        }

        public void setWorktimeandarea(TextView worktimeandarea) {
            this.worktimeandarea = worktimeandarea;
        }

        public TextView getWorkstatus() {
            return workstatus;
        }

        public void setWorkstatus(TextView workstatus) {
            this.workstatus = workstatus;
        }

        public TextView getSalaryperhour() {
            return salaryperhour;
        }

        public void setSalaryperhour(TextView salaryperhour) {
            this.salaryperhour = salaryperhour;
        }

        public TextView getPaymentway() {
            return paymentway;
        }

        public void setPaymentway(TextView paymentway) {
            this.paymentway = paymentway;
        }

        public AppCompatButton getBtn_getPhone() {
            return btn_getPhone;
        }

        public void setBtn_getPhone(AppCompatButton btn_getPhone) {
            this.btn_getPhone = btn_getPhone;
        }

        public AppCompatButton getBtn_cancleSignup() {
            return btn_cancleSignup;
        }

        public void setBtn_cancleSignup(AppCompatButton btn_cancleSignup) {
            this.btn_cancleSignup = btn_cancleSignup;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType==NORMAL_VIEW){
            View normalView= LayoutInflater.from(context).inflate(R.layout.activity_mysignup_item,null); //定义view
            NormalViewHolder Normalholder=new NormalViewHolder(normalView);
            //接下来是控件的引用声明
            Normalholder.workname=(TextView) normalView.findViewById(R.id.workname);
            Normalholder.worktimeandarea=(TextView) normalView.findViewById(R.id.worktimeandarea);
            Normalholder.workstatus=(TextView) normalView.findViewById(R.id.workstatus);
            Normalholder.salaryperhour=(TextView) normalView.findViewById(R.id.salaryperhour);
            Normalholder.paymentway=(TextView) normalView.findViewById(R.id.paymentway);
            Normalholder.btn_getPhone=(AppCompatButton) normalView.findViewById(R.id.btn_getPhone);
            Normalholder.btn_cancleSignup=(AppCompatButton) normalView.findViewById(R.id.btn_cancleSignup);
            return Normalholder;
        }else{
            return null;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        try{
            if(holder instanceof MysignupAdapter.NormalViewHolder){
                MysignupAdapter.NormalViewHolder normalViewHolder=(MysignupAdapter.NormalViewHolder) holder;
                Signup data=dataList.get(position);
                Log.d("data",data.toString());//测试

                //填充工作名称
                normalViewHolder.workname.setText(data.getWorkName());
                Log.d("workname",normalViewHolder.workname.getText().toString());
                //填充工作时间、地点
                normalViewHolder.worktimeandarea.setText(data.getWorkTime()+" | "+data.getWorkArea());
                Log.d("worktimeandarea",normalViewHolder.worktimeandarea.getText().toString());
                //填充工作状态
                normalViewHolder.workstatus.setText("√"+data.getSignupStatus());
                Log.d("workstatus",normalViewHolder.workstatus.getText().toString());
                //填充时薪
                normalViewHolder.salaryperhour.setText(data.getSalaryPerHour()+"/小时");
                Log.d("salaryperhour",normalViewHolder.salaryperhour.getText().toString());
                //填充结付方式
                normalViewHolder.paymentway.setText(data.getPaymentWay());
                Log.d("paymentway",normalViewHolder.paymentway.getText().toString());

                //如果是已录取或者已结束就修改按钮的内容
                if(data.getSignupStatus().equals("已录取") || data.getSignupStatus().equals("已结束")){
                    normalViewHolder.btn_cancleSignup.setVisibility(View.GONE);
                    if(data.getSignupStatus().equals("已结束")){
                        if(!data.getIsLike().equals("已点赞")){
                            normalViewHolder.btn_getPhone.setText("点赞");
                        }else{
                            normalViewHolder.btn_getPhone.setText("已点赞");
                            normalViewHolder.btn_getPhone.setBackgroundColor(Color.rgb(184, 184, 184));
                            normalViewHolder.btn_getPhone.postInvalidate();
                        }
                    }
                }

                //如果是已取消就隐藏两个按钮
                if(data.getSignupStatus().equals("已取消")){
                    normalViewHolder.btn_cancleSignup.setVisibility(View.GONE);
                    normalViewHolder.btn_getPhone.setVisibility(View.GONE);
                }

                //监视按钮取消报名
                normalViewHolder.btn_cancleSignup.setOnClickListener(view1 -> {
                    DeleteSignup(mDatabase,data.getSignupID());
                    //取消报名后刷新
                    normalViewHolder.btn_cancleSignup.setVisibility(View.GONE);
                    normalViewHolder.btn_getPhone.setVisibility(View.GONE);
                    normalViewHolder.workstatus.setText("已取消");
                    normalViewHolder.btn_cancleSignup.postInvalidate();
                    normalViewHolder.btn_getPhone.postInvalidate();
                    normalViewHolder.workstatus.postInvalidate();
                });

                //监视按钮点赞 或 联系发布方
                normalViewHolder.btn_getPhone.setOnClickListener(view1 -> {
                    if(data.getSignupStatus().equals("已结束")){
                        like(mDatabase,data.getPublisherInfo(),data.getSignupID());
                        normalViewHolder.btn_getPhone.setText("已点赞");
                        normalViewHolder.btn_getPhone.setBackgroundColor(Color.rgb(184, 184, 184));
                        normalViewHolder.btn_getPhone.postInvalidate();
                        normalViewHolder.btn_getPhone.setClickable(false); //点赞后设置为不可点赞
                        normalViewHolder.btn_getPhone.setEnabled(false); //点赞后设置为不可点赞
                        normalViewHolder.btn_getPhone.postInvalidate();//生效
                    }else if(data.getSignupStatus().equals("已报名") || data.getSignupStatus().equals("已录取")){
                        Toast.makeText(this.context, "联系方式："+data.getPublisherInfo() , Toast.LENGTH_SHORT).show();
                    }
                });

            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //点赞函数
    public void like(SQLiteDatabase mDatabase,String publisherinfo,long signupID){
        //根据发布方联系方式在tbl_publisher表中找到对应的那个发布方，更新该发布方numOfLikes
        Cursor c=mDatabase.query("tbl_publisher", null, "Call=?", new String[]{publisherinfo}, null, null,null);
        c.moveToFirst();//游标移到第一条记录
        // 打印记录
        String[] res = new String[30];
        while(!c.isAfterLast()){
            String rowResults = "||";
            for (int i = 0; i < c.getColumnCount(); i++) {
                rowResults = rowResults.concat(c.getString(i) + "||");
                res[i]=c.getString(i);
            }
            c.moveToNext();
        }
        c.close();//关闭游标工厂
        String pid=res[0];
        String name=res[1];
        String info=res[3];
        int numoflikes=Integer.parseInt(res[4]);

        ContentValues values = new ContentValues();
        values.put("NumOfLikes", ++numoflikes);
        mDatabase.update("tbl_publisher",values,"PublisherID=?",new String[]{pid});
        mDatabase.update("tbl_work",values,"PublisherName=? AND PublisherInfo=?",new String[]{name,info});
        Log.d("update numOflikes","成功更新被点赞数");
        ContentValues values2 = new ContentValues();
        values2.put("IsLike", "已点赞");
        mDatabase.update("tbl_signup",values2,"SignupID=?",new String[]{signupID + ""});
    }

    //取消报名函数
    public void DeleteSignup(SQLiteDatabase mDatabase,long signupID){
        ContentValues values = new ContentValues();
        values.put("SignupStatus", "已取消");
        mDatabase.update("tbl_signup",values,"SignupID=?",new String[]{signupID + ""});
    }

    //重写getItemCount方法
    @Override
    public int getItemCount() {
        return dataList.size();
    }

    //重写getItemViewType方法
    @Override
    public int getItemViewType(int position){
        if(position>=0){
            return NORMAL_VIEW;
        }else{
            return NORMAL_VIEW;
        }
    }
}
